home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / SITES / LITTLE / P3SRC.ZIP / ATARI / MEM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-27  |  23.2 KB  |  940 lines

  1. /****************************************************************************
  2. *                mem.c
  3. *
  4. *  This module contains the code for our own memory allocation/deallocation,
  5. *  providing memory tracing, statistics, and garbage collection options.
  6. *
  7. *  from Persistence of Vision(tm) Ray Tracer
  8. *  Copyright 1996 Persistence of Vision Team
  9. *---------------------------------------------------------------------------
  10. *  NOTICE: This source code file is provided so that users may experiment
  11. *  with enhancements to POV-Ray and to port the software to platforms other
  12. *  than those supported by the POV-Ray Team.  There are strict rules under
  13. *  which you are permitted to use this file.  The rules are in the file
  14. *  named POVLEGAL.DOC which should be distributed with this file. If
  15. *  POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  16. *  Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  17. *  Forum.  The latest version of POV-Ray may be found there as well.
  18. *
  19. * This program is based on the popular DKB raytracer version 2.12.
  20. * DKBTrace was originally written by David K. Buck.
  21. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  22. *
  23. *****************************************************************************/
  24.  
  25. #include "frame.h"
  26. #include "povproto.h"           /* Error() */
  27.  
  28. #include "mem.h"
  29. #include "parse.h"              /* MAError() */
  30. #include "povray.h"             /* stats[] global var */
  31.  
  32.  
  33. /************************************************************************
  34. * AUTHOR
  35. *
  36. *   Steve Anger:70714,3113
  37. *
  38. * DESCRIPTION
  39. *
  40. This module replaces the memory allocation calls malloc, calloc, realloc
  41. and free with the macros POV_MALLOC, POV_CALLOC, POV_REALLOC, and POV_FREE.
  42. These macros work the same as the standard C functions except that the
  43. POV_xALLOC functions also take a message as the last parameter and
  44. automatically call MAError(msg) if the allocation fails. That means that
  45. instead of writing
  46.  
  47.   if ((New = malloc(sizeof(*New))) == NULL)
  48.     {
  49.     MAError ("new object");
  50.     }
  51.  
  52. you'd just use
  53.  
  54.   New = POV_MALLOC (sizeof(*New), "new object");
  55.  
  56. This also expands the function of the macros to include error checking and
  57. memory tracking.
  58.  
  59. The following macros need to be defined in config.h, depending of what
  60. features the compile needs:
  61.  
  62. #define MEM_TAG     - Enables memory tag debugging
  63. --------------------------------------------------
  64. Memory tag debugging adds a 32-bit identifier to the beginning of each
  65. allocated memory block and erases it after the block has been free'd. This
  66. lets POV_FREE verify that the block it's freeing is valid and issue an
  67. error message if it isn't. Makes it easy to find those nasty double free's
  68. which usually corrupt the heap.
  69.  
  70. #define MEM_RECLAIM - Enables garbage collection
  71. ------------------------------------------------
  72. Garbage collection maintains a list of all currently allocated memory so
  73. that it can be free'd when the program exits. Normally POV-Ray will free all
  74. of its memory on its own, however abnormal exits such as parser errors or
  75. user aborts bypass the destructors. There are four functions which control
  76. the garbage collection:
  77.  
  78. mem_init()
  79.   Initializes global variables used by the garbage collection routines.
  80.   This function should be called once before any memory allocation functions
  81.   are called.
  82.  
  83. mem_mark()
  84.   Starts a new memory pool. The next call to mem_release() will only release
  85.   memory allocated after this call.
  86.  
  87. mem_release (int LogFile)
  88.   Releases all unfree'd memory allocated since the last call to mem_mark().
  89.   The LogFile parameter determines if it dumps the list of unfree'd memory to
  90.   a file.
  91.  
  92. mem_release_all (int LogFile)
  93.   Releases all unfree'd memory allocated since the program started running.
  94.  
  95. POV-Ray only uses the mem_release_all() function however mem_mark() and
  96. mem_release() might be useful for implenting a leak-free animation loop.
  97.  
  98. #define MEM_TRACE   - Enables garbage collection and memory tracing
  99. -------------------------------------------------------------------
  100. Memory tracing stores the file name and line number for ever POV_xALLOC
  101. call and dumps a list of unfree'd blocks when POV-Ray terminates.
  102.  
  103. #define MEM_STATS 1    - enables tracking of memory statistics
  104. -------------------------------------------------------------------
  105. Memory statistics enables routines that will track overall memory usage.
  106. After all memory allocation/deallocation has taken place, and before you
  107. re-initialize everything with another mem_init() call, you can call some
  108. accessor routines to determine how memory was used.  Setting MEM_STATS
  109. to 1 only tracks peak memory usage.  Setting it to 2 additionally tracks
  110. number of calls to malloc/free and some other statistics.
  111. *
  112. * CHANGES
  113. *
  114. *   Aug 1995 : Steve Anger - Creation.
  115. *   Apr 1996 : Eduard Schwan - Added MEM_STATS code
  116. **************************************************************************/
  117.  
  118.  
  119. /****************************************************************************/
  120. /* Allow user definable replacements for memory functions                   */
  121. /****************************************************************************/
  122. #ifndef MALLOC
  123. #define MALLOC malloc
  124. #endif
  125.  
  126. #ifndef CALLOC
  127. #define CALLOC calloc
  128. #endif
  129.  
  130. #ifndef REALLOC
  131. #define REALLOC realloc
  132. #endif
  133.  
  134. #ifndef FREE
  135. #define FREE free
  136. #endif
  137.  
  138.  
  139. /****************************************************************************/
  140. /* internal use                                                             */
  141. /****************************************************************************/
  142.  
  143. /* if TRACE is on, the RECLAIM must also be on */
  144. #if defined(MEM_TRACE) && !defined (MEM_RECLAIM)
  145. #define MEM_RECLAIM
  146. #endif
  147.  
  148. /* This is the filename created for memory leakage information */
  149. #if defined(MEM_TRACE)
  150. #define MEM_LOG_FNAME   "Memory.Log"
  151. #endif
  152.  
  153. /* determine if we need to add a header to our memory records */
  154. #if defined(MEM_TAG) || defined(MEM_RECLAIM) || defined(MEM_TRACE)
  155. #define MEM_HEADER
  156. #endif
  157.  
  158. #define MEMNODE struct mem_node
  159.  
  160. #if defined(MEM_HEADER)
  161.  
  162. struct mem_node
  163. {
  164.  
  165. #if defined(MEM_TAG)
  166.   long tag;
  167. #endif /* MEM_TAG */
  168.  
  169. #if defined(MEM_RECLAIM)
  170.   short poolno;
  171.   MEMNODE *prev;
  172.   MEMNODE *next;
  173. #endif /* MEM_RECLAIM */
  174.  
  175. #if defined(MEM_TRACE) || defined(MEM_STATS)
  176.   size_t size;
  177. #endif
  178. #if defined(MEM_TRACE)
  179.   char *file;
  180.   int line;
  181. #endif /* MEM_TRACE */
  182. };
  183. #endif /* MEM_HEADER */
  184.  
  185.  
  186. #if defined(MEM_RECLAIM)
  187. static int poolno = 0;
  188. static MEMNODE *memlist = NULL;
  189. #endif
  190.  
  191.  
  192. static int leak_msg = FALSE;
  193.  
  194.  
  195. #if defined(MEM_HEADER)
  196. #define NODESIZE ((sizeof(MEMNODE)+3)/4)*4  /* Align memory on 4 byte boundary */
  197. #else
  198. #define NODESIZE 0
  199. #endif
  200.  
  201.  
  202. #if defined(MEM_RECLAIM)
  203. static void add_node(MEMNODE * node);
  204. static void remove_node(MEMNODE * node);
  205. #endif
  206.  
  207.  
  208. #if defined(MEM_TAG)
  209. /* the tag value that marks our used memory */
  210. #define MEMTAG_VALUE   0x4D546167L
  211.  
  212. static int mem_check_tag(MEMNODE * node);
  213.  
  214. #endif
  215.  
  216.  
  217. #if defined(MEM_RECLAIM)
  218. static long num_nodes;          /* keep track of valence of node list */
  219. #endif /* MEM_RECLAIM */
  220.  
  221.  
  222. #if defined(MEM_STATS)
  223.  
  224. typedef struct MemStats_Struct MEMSTATS;
  225.  
  226. struct MemStats_Struct
  227. {
  228.   size_t   smallest_alloc;    /* smallest # of bytes in one malloc() */
  229.   size_t   largest_alloc;     /* largest # of bytes in one malloc() */
  230.   size_t   current_mem_usage; /* current total # of bytes allocated */
  231.   size_t   largest_mem_usage; /* peak total # of bytes allocated */
  232. #if (MEM_STATS>=2)
  233.   /* could add a running average size too, someday */
  234.   long int total_allocs;      /* total # of alloc calls */
  235.   long int total_frees;       /* total # of free calls */
  236.   char    *smallest_file;     /* file name of largest alloc */
  237.   int      smallest_line;     /* file line of largest alloc */
  238.   char    *largest_file;      /* file name of largest alloc */
  239.   int      largest_line;      /* file line of largest alloc */
  240. #endif
  241. };
  242.  
  243. /* keep track of memory allocation statistics */
  244. static MEMSTATS mem_stats;
  245.  
  246. /* local prototypes */
  247. static void mem_stats_init PARAMS((void));
  248. static void mem_stats_alloc PARAMS((size_t nbytes, char *file, int line));
  249. static void mem_stats_free PARAMS((size_t nbytes));
  250.  
  251. #endif
  252.  
  253.  
  254. /****************************************************************************/
  255. void mem_init()
  256. {
  257. #if defined(MEM_RECLAIM)
  258.   num_nodes = 0;
  259.   poolno = 0;
  260.   memlist = NULL;
  261. #endif
  262. #if defined(MEM_STATS)
  263.   mem_stats_init();
  264. #endif
  265.   leak_msg = FALSE;
  266. }
  267.  
  268.  
  269. #if defined(MEM_TAG)
  270. /****************************************************************************/
  271. /* return TRUE if pointer is non-null and has a valid tag */
  272. static int mem_check_tag(node)
  273. MEMNODE *node;
  274. {
  275.   int isOK = FALSE;
  276.  
  277.   if (node != NULL)
  278.     if (node->tag == MEMTAG_VALUE)
  279.       isOK = TRUE;
  280.   return isOK;
  281. }
  282.  
  283. #endif /* MEM_TAG */
  284.  
  285.  
  286. /****************************************************************************/
  287. void *pov_malloc(size, file, line, msg)
  288. size_t size;
  289. char *file;
  290. int line;
  291. char *msg;
  292. {
  293.   void *block;
  294.   size_t totalsize;
  295. #if defined(MEM_HEADER)
  296.   MEMNODE *node;
  297. #endif
  298.  
  299. #if defined(MEM_HEADER)
  300.   if (size == 0)
  301.   {
  302.     Error("Attempt to malloc zero size block (File: %s Line: %d).\n", file, line);
  303.   }
  304. #endif
  305.  
  306.   totalsize=size+NODESIZE; /* number of bytes allocated in OS */
  307.  
  308.   block = (void *)MALLOC(totalsize);
  309.  
  310.   if (block == NULL)
  311.     MAError(msg, (long)size);
  312.  
  313. #if defined(MEM_HEADER)
  314.   node = (MEMNODE *) block;
  315. #endif
  316.  
  317. #if defined(MEM_TAG)
  318.   node->tag = MEMTAG_VALUE;
  319. #endif
  320.  
  321. #if defined(MEM_TRACE) || defined(MEM_STATS)
  322.   node->size = totalsize;
  323. #endif
  324. #if defined(MEM_TRACE)
  325.   node->file = file;
  326.   node->line = line;
  327. #endif
  328.  
  329. #if defined(MEM_RECLAIM)
  330.   add_node(node);
  331. #endif
  332.  
  333. #if defined(MEM_STATS)
  334.   mem_stats_alloc(totalsize, file, line);
  335. #endif
  336.  
  337.   return (void *)((char *)block + NODESIZE);
  338. }
  339.  
  340.  
  341. /****************************************************************************/
  342. void *pov_calloc(nitems, size, file, line, msg)
  343. size_t nitems;
  344. size_t size;
  345. char *file;
  346. int line;
  347. char *msg;
  348. {
  349.   void *block;
  350.   size_t actsize;
  351.   size_t totalsize; /* number of bytes allocated in OS */
  352. #if defined(MEM_HEADER)
  353.   MEMNODE *node;
  354. #endif
  355.  
  356.   actsize=nitems*size;
  357.   totalsize=actsize+NODESIZE;
  358.  
  359. #if defined(MEM_HEADER)
  360.   if (actsize == 0)
  361.   {
  362.     Error("Attempt to calloc zero size block (File: %s Line: %d).\n", file, line);
  363.   }
  364. #endif
  365.  
  366.   block = (void *)MALLOC(totalsize);
  367.  
  368.   if (block == NULL)
  369.     MAError(msg, actsize);
  370.  
  371.   memset(block, 0, totalsize);
  372.  
  373. #if defined(MEM_HEADER)
  374.   node = (MEMNODE *) block;
  375. #endif
  376.  
  377. #if defined(MEM_TAG)
  378.   node->tag = MEMTAG_VALUE;
  379. #endif
  380.  
  381. #if defined(MEM_TRACE) || defined(MEM_STATS)
  382.   node->size = totalsize;
  383. #endif
  384. #if defined(MEM_TRACE)
  385.   node->file = file;
  386.   node->line = line;
  387. #endif
  388.  
  389. #if defined(MEM_RECLAIM)
  390.   add_node(node);
  391. #endif
  392.  
  393. #if defined(MEM_STATS)
  394.   mem_stats_alloc(totalsize, file, line);
  395. #endif
  396.  
  397.   return (void *)((char *)block + NODESIZE);
  398. }
  399.  
  400.  
  401. /****************************************************************************/
  402. void *pov_realloc(ptr, size, file, line, msg)
  403. void *ptr;
  404. size_t size;
  405. char *file;
  406. int line;
  407. char *msg;
  408. {
  409.   void *block;
  410. #if defined(MEM_STATS)
  411.   size_t oldsize;
  412. #endif
  413.  
  414. #if defined(MEM_HEADER)
  415.   MEMNODE *node;
  416.  
  417. #endif
  418. #if defined(MEM_RECLAIM)
  419.   MEMNODE *prev;
  420.   MEMNODE *next;
  421.  
  422. #endif
  423.  
  424. #if defined(MEM_HEADER)
  425.   if (size == 0)
  426.   {
  427.     Error("Attempt to realloc zero size block (File: %s Line: %d).\n", file, line);
  428.   }
  429. #endif
  430.  
  431.   if (ptr == NULL)
  432.     return pov_malloc(size, file, line, msg);
  433.  
  434.   block = (void *)((char *)ptr - NODESIZE);
  435.  
  436. #if defined(MEM_HEADER)
  437.   node = (MEMNODE *) block;
  438. #endif
  439.  
  440. #if defined(MEM_TAG)
  441.   if (node->tag != MEMTAG_VALUE)
  442.     Error("Attempt to realloc invalid block (File: %s Line: %d).\n", file, line);
  443.  
  444.   node->tag = ~node->tag;
  445. #endif
  446.  
  447. #if defined(MEM_RECLAIM)
  448.   prev = node->prev;
  449.   next = node->next;
  450. #endif
  451.  
  452.   block = (void *)REALLOC(block, NODESIZE + size);
  453.  
  454.   if (block == NULL)
  455.     MAError(msg, (long)size);
  456.  
  457. #if defined(MEM_STATS)
  458.   /* REALLOC does an implied FREE... */
  459.   oldsize = ((MEMNODE *)block)->size;
  460.   mem_stats_free(oldsize);
  461.   /* ...and an implied MALLOC... */
  462.   mem_stats_alloc(NODESIZE + size, file, line);
  463. #endif
  464.  
  465. #if defined(MEM_HEADER)
  466.   node = (MEMNODE *) block;
  467. #endif
  468.  
  469. #if defined(MEM_TAG)
  470.   node->tag = MEMTAG_VALUE;
  471. #endif
  472.  
  473. #if defined(MEM_TRACE) || defined(MEM_STATS)
  474.   node->size = size + NODESIZE;
  475. #endif
  476. #if defined(MEM_TRACE)
  477.   node->file = file;
  478.   node->line = line;
  479. #endif
  480.  
  481. #if defined(MEM_RECLAIM)
  482.   if (prev == NULL)
  483.     memlist = node;
  484.   else
  485.     prev->next = node;
  486.   if (node->next != NULL)
  487.     node->next->prev = node;
  488.   if (next != NULL)
  489.     next->prev = node;
  490. #endif
  491.  
  492.   return (void *)((char *)block + NODESIZE);
  493. }
  494.  
  495.  
  496. /****************************************************************************/
  497. void pov_free(ptr, file, line)
  498. void *ptr;
  499. char *file;
  500. int line;
  501. {
  502.   void *block;
  503.  
  504. #if defined(MEM_HEADER)
  505.   MEMNODE *node;
  506.  
  507. #endif
  508.  
  509.   if (ptr == NULL)
  510.     Error("Attempt to free NULL pointer (File: %s Line: %d).\n", file, line);
  511.  
  512.   block = (void *)((char *)ptr - NODESIZE);
  513.  
  514. #if defined(MEM_HEADER)
  515.   node = (MEMNODE *) block;
  516. #endif
  517.  
  518. #if defined(MEM_TAG)
  519.   if (node->tag == ~MEMTAG_VALUE)
  520.   {
  521.     Warning(0.0, "Attempt to free already free'd block (File: %s Line: %d).\n", file, line);
  522.     return;
  523.   }
  524.   else if (node->tag != MEMTAG_VALUE)
  525.   {
  526.     Warning(0.0, "Attempt to free invalid block (File: %s Line: %d).\n", file, line);
  527.     return;
  528.   }
  529.  
  530. #endif
  531.  
  532. #if defined(MEM_RECLAIM)
  533.   remove_node(node);
  534. #endif
  535.  
  536. #if defined(MEM_TAG)
  537.   /* do this After remove_node, so remove_node can check validity of nodes */
  538.   node->tag = ~node->tag;
  539. #endif
  540.  
  541. #if defined(MEM_STATS)
  542.   mem_stats_free(((MEMNODE*)block)->size);
  543. #endif
  544.  
  545.   FREE(block);
  546. }
  547.  
  548.  
  549. /****************************************************************************/
  550. /* Starts a new memory pool. The next mem_release() call will
  551.    only release memory allocated after this call. */
  552. void mem_mark()
  553. {
  554. #if defined(MEM_RECLAIM)
  555.   poolno++;
  556. #endif
  557. }
  558.  
  559.  
  560. /****************************************************************************/
  561. /* Releases all unfree'd memory from current memory pool */
  562. void mem_release(LogFile)
  563. int LogFile;
  564. {
  565. #if defined(MEM_RECLAIM)
  566.   FILE *f = NULL;
  567.   MEMNODE *p, *tmp;
  568.   size_t totsize;
  569.  
  570.   p = memlist;
  571.   totsize = 0;
  572.  
  573. #if defined(MEM_TRACE)
  574.   if (LogFile)
  575.   {
  576.     if (p != NULL && (p->poolno == poolno))
  577.       f = fopen(MEM_LOG_FNAME, APPEND_FILE_STRING);
  578.   }
  579. #endif /* MEM_TRACE */
  580.  
  581.   while (p != NULL && (p->poolno == poolno))
  582.   {
  583. #if defined(MEM_TRACE)
  584.  
  585. #if defined(MEM_TAG)
  586.     if (!mem_check_tag(p))
  587.       Debug_Info("mem_release(): Memory pointer corrupt!\n");
  588. #endif /* MEM_TAG */
  589.  
  590.     totsize += (p->size-NODESIZE);
  591.     if (LogFile)
  592.     {
  593.       if (!leak_msg)
  594.       {
  595.         Debug_Info("Memory leakage detected, see file '%s' for list\n",MEM_LOG_FNAME);
  596.         leak_msg = TRUE;
  597.       }
  598.  
  599.       if (f != NULL)
  600.         fprintf(f, "File:%13s  Line:%4d  Size:%lu\n", p->file, p->line, (unsigned long)(p->size-NODESIZE));
  601.     }
  602. #endif /* MEM_TRACE */
  603.  
  604. #if defined(MEM_STATS)
  605.     mem_stats_free(p->size);
  606. #endif
  607.  
  608.     tmp = p;
  609.     p = p->next;
  610.     remove_node(tmp);
  611.     FREE(tmp);
  612.   }
  613.  
  614.   if (f != NULL)
  615.     fclose(f);
  616.  
  617.   if (totsize > 0)
  618.     Debug_Info("%lu bytes reclaimed (pool #%d)\n", totsize, poolno);
  619.  
  620.   if (poolno > 0)
  621.     poolno--;
  622.  
  623. #if defined(MEM_STATS)
  624.   /* reinitialize the stats structure for next time through */
  625.   mem_stats_init();
  626. #endif
  627.  
  628. #endif /* MEM_RECLAIM */
  629. }
  630.  
  631.  
  632. /****************************************************************************/
  633. /* Released all unfree'd memory from all pools */
  634. void mem_release_all(LogFile)
  635. int LogFile;
  636. {
  637. #if defined(MEM_RECLAIM)
  638.   FILE *f = NULL;
  639.   MEMNODE *p, *tmp;
  640.   size_t totsize;
  641.  
  642.   Status_Info("Reclaiming memory\n");
  643.  
  644.   p = memlist;
  645.   totsize = 0;
  646.  
  647. #if defined(MEM_TRACE)
  648.   if (LogFile)
  649.   {
  650.     if (p != NULL)
  651.       f = fopen(MEM_LOG_FNAME, APPEND_FILE_STRING);
  652.   }
  653. #endif
  654.  
  655.   while (p != NULL)
  656.   {
  657. #if defined(MEM_TRACE)
  658.  
  659.     #if defined(MEM_TAG)
  660.     if (!mem_check_tag(p))
  661.       Debug_Info("mem_release_all(): Memory pointer corrupt!\n");
  662.     #endif /* MEM_TAG */
  663.  
  664.     totsize += (p->size-NODESIZE);
  665.     if (LogFile)
  666.     {
  667.       if (!leak_msg)
  668.       {
  669.         Debug_Info("Memory leakage detected, see file '%s' for list\n",MEM_LOG_FNAME);
  670.         leak_msg = TRUE;
  671.       }
  672.  
  673.       if (f != NULL)
  674.         fprintf(f, "File:%13s  Line:%4d  Size:%lu\n", p->file, p->line, (unsigned long)(p->size-NODESIZE));
  675.     }
  676. #endif
  677.  
  678. #if defined(MEM_STATS)
  679.     /* This is after we have printed stats, and this may slow us down a little,      */
  680.     /* so we may want to simply re-initialize the mem-stats at the end of this loop. */
  681.     mem_stats_free(p->size);
  682. #endif
  683.  
  684.     tmp = p;
  685.     p = p->next;
  686.     remove_node(tmp);
  687.     FREE(tmp);
  688.   }
  689.  
  690.   if (f != NULL)
  691.     fclose(f);
  692.  
  693.   if (totsize > 0)
  694.     Debug_Info("\n%lu bytes reclaimed\n", totsize);
  695.  
  696.   poolno = 0;
  697. #endif
  698.  
  699. #if defined(MEM_STATS)
  700.   /* reinitialize the stats structure for next time through */
  701.   mem_stats_init();
  702. #endif
  703.  
  704. }
  705.  
  706.  
  707. /****************************************************************************/
  708. #if defined(MEM_RECLAIM)
  709. /* Adds a new node to the 'allocated' list */
  710. static void add_node(node)
  711. MEMNODE *node;
  712. {
  713.  
  714. #if defined(MEM_TAG)
  715.   if (!mem_check_tag(node))
  716.     Debug_Info("add_node(): Memory pointer corrupt!\n");
  717. #endif /* MEM_TAG */
  718.  
  719.   if (memlist == NULL)
  720.   {
  721.     memlist = node;
  722.     node->poolno = poolno;
  723.     node->prev = NULL;
  724.     node->next = NULL;
  725.     num_nodes = 0;
  726.   }
  727.   else
  728.   {
  729.     memlist->prev = node;
  730.     node->poolno = poolno;
  731.     node->prev = NULL;
  732.     node->next = memlist;
  733.     memlist = node;
  734.   }
  735.   num_nodes++;
  736. }
  737.  
  738.  
  739. /****************************************************************************/
  740. /* Detatches a node from the 'allocated' list but doesn't free it */
  741. static void remove_node(node)
  742. MEMNODE *node;
  743. {
  744.  
  745. #if defined(MEM_TAG)
  746.   if (!mem_check_tag(node))
  747.     Debug_Info("remove_node(): Memory pointer corrupt!\n");
  748. #endif /* MEM_TAG */
  749.  
  750.   num_nodes--;
  751.   if (node->prev != NULL)
  752.     node->prev->next = node->next;
  753.  
  754.   if (node->next != NULL)
  755.     node->next->prev = node->prev;
  756.  
  757.   if (memlist == node)
  758.   {
  759. #if defined(MEM_TAG)
  760.     /* check node->next if it is non-null, to insure it is safe to assign. */
  761.     /* if it is null, it is safe since it is the last in the list. */
  762.     if (node->next)
  763.       if (!mem_check_tag(node->next))
  764.         Debug_Info("remove_node(): memlist pointer corrupt!\n");
  765. #endif /* MEM_TAG */
  766.  
  767.     memlist = node->next;
  768.   }
  769.  
  770. }
  771.  
  772. #endif /* MEM_RECLAIM */
  773.  
  774.  
  775. /****************************************************************************/
  776. /* A memcpy routine that works even if the copied areas overlap             */
  777. /****************************************************************************/
  778. void pov_memcpy (dest, src, length)
  779. char *dest, *src;
  780. int length;
  781. {
  782.   int i;
  783.   
  784.   if (src < dest)
  785.   {
  786.     if (&(src[length])>=dest)
  787.     {
  788.       for (i=length-1; i>=0; i--)
  789.       {
  790.         dest[i]=src[i];
  791.       }
  792.     }
  793.     else
  794.     {
  795.       memcpy(dest,src,length);
  796.     }
  797.   }
  798. }
  799.  
  800.  
  801. /****************************************************************************/
  802. /* Memory Statistics gathering routines                                     */
  803. /****************************************************************************/
  804.  
  805. #if defined(MEM_STATS)
  806.  
  807. /****************************************************************************/
  808. static void mem_stats_init()
  809. {
  810.   mem_stats.smallest_alloc    = 65535;  /* Must be an unsigned number */
  811.   mem_stats.largest_alloc     = 0;
  812.   mem_stats.current_mem_usage = 0;
  813.   mem_stats.largest_mem_usage = 0;
  814. #if (MEM_STATS>=2)
  815.   mem_stats.total_allocs      = 0;
  816.   mem_stats.total_frees       = 0;
  817.   mem_stats.largest_file      = "none";
  818.   mem_stats.largest_line      = -1;
  819.   mem_stats.smallest_file     = "none";
  820.   mem_stats.smallest_line     = -1;
  821. #endif
  822. }
  823.  
  824. /****************************************************************************/
  825. /* update appropriate fields when an allocation takes place                 */
  826. static void mem_stats_alloc(nbytes, file, line)
  827. size_t nbytes;
  828. char *file;
  829. int line;
  830. {
  831.   /* update the fields */
  832.   if ((mem_stats.smallest_alloc<0) || (nbytes<mem_stats.smallest_alloc))
  833.   {
  834.     mem_stats.smallest_alloc = nbytes;
  835. #if (MEM_STATS>=2)
  836.     mem_stats.smallest_file = file;
  837.     mem_stats.smallest_line = line;
  838. #endif
  839.   }
  840.  
  841.   if (nbytes>mem_stats.largest_alloc)
  842.   {
  843.     mem_stats.largest_alloc = nbytes;
  844. #if (MEM_STATS>=2)
  845.     mem_stats.largest_file = file;
  846.     mem_stats.largest_line = line;
  847. #endif
  848.   }
  849.  
  850. #if (MEM_STATS>=2)
  851.   mem_stats.total_allocs++;
  852. #endif
  853.  
  854.   mem_stats.current_mem_usage += nbytes;
  855.  
  856.   if (mem_stats.current_mem_usage>mem_stats.largest_mem_usage)
  857.   {
  858.     mem_stats.largest_mem_usage = mem_stats.current_mem_usage;
  859.   }
  860.  
  861. }
  862.  
  863. /****************************************************************************/
  864. /* update appropriate fields when a free takes place                        */
  865. static void mem_stats_free(nbytes)
  866. size_t nbytes;
  867. {
  868.   /* update the fields */
  869.   mem_stats.current_mem_usage -= nbytes;
  870. #if (MEM_STATS>=2)
  871.   mem_stats.total_frees++;
  872. #endif
  873. }
  874.  
  875. /****************************************************************************/
  876. /* Level 1                                                                  */
  877.  
  878. /****************************************************************************/
  879. size_t mem_stats_smallest_alloc()
  880. {
  881.   return mem_stats.smallest_alloc;
  882. }
  883. /****************************************************************************/
  884. size_t mem_stats_largest_alloc()
  885. {
  886.   return mem_stats.largest_alloc;
  887. }
  888. /****************************************************************************/
  889. size_t mem_stats_current_mem_usage()
  890. {
  891.   return mem_stats.current_mem_usage;
  892. }
  893. /****************************************************************************/
  894. size_t mem_stats_largest_mem_usage()
  895. {
  896.   return mem_stats.largest_mem_usage;
  897. }
  898.  
  899. /****************************************************************************/
  900. /* Level 2                                                                  */
  901.  
  902. #if (MEM_STATS>=2)
  903.  
  904. /****************************************************************************/
  905. char *mem_stats_smallest_file()
  906. {
  907.   return mem_stats.smallest_file;
  908. }
  909. /****************************************************************************/
  910. int mem_stats_smallest_line()
  911. {
  912.   return mem_stats.smallest_line;
  913. }
  914. /****************************************************************************/
  915. char *mem_stats_largest_file()
  916. {
  917.   return mem_stats.largest_file;
  918. }
  919. /****************************************************************************/
  920. int mem_stats_largest_line()
  921. {
  922.   return mem_stats.largest_line;
  923. }
  924. /****************************************************************************/
  925. long int mem_stats_total_allocs()
  926. {
  927.   return mem_stats.total_allocs;
  928. }
  929. /****************************************************************************/
  930. long int mem_stats_total_frees()
  931. {
  932.   return mem_stats.total_frees;
  933. }
  934.  
  935. #endif
  936.  
  937. #endif /* MEM_STATS */
  938.  
  939.  
  940.